package cn.yeziji.forum.config.quartz;

import cn.yeziji.forum.job.RedisDetailsJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * quartz 定时任务配置
 *
 * @author gzkemays
 * @since 2021/12/23 17:51
 */
//@Configuration
public class QuartzConfig {
  private static String JOB_GROUP_NAME = "PJB_JOBGROUP_NAME";
  private static String TRIGGER_GROUP_NAME = "PJB_TRIGGERGROUP_NAME";

  /** 配置定时任务信息 */
  @Bean
  public JobDetail syncUserJobDetail() {
    return JobBuilder.newJob(RedisDetailsJob.class)
        .withIdentity("syncUserJobDetail", JOB_GROUP_NAME)
        // 设置参数（键值对）
        .usingJobData("title", "redis")
        .usingJobData("content", "123")
        // 即使没有Trigger关联时，也不需要删除该JobDetail
        .storeDurably()
        .build();
  }

  /** 定时任务触发器 */
  @Bean
  public Trigger syncUserJobTrigger() {
    // 每隔5秒执行一次
    CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
    // 创建触发器
    return TriggerBuilder.newTrigger()
        // 关联上述的JobDetail
        .forJob(syncUserJobDetail())
        // 给Trigger起个名字
        .withIdentity("syncUserJobTrigger", TRIGGER_GROUP_NAME)
        .withSchedule(cronScheduleBuilder)
        .build();
  }
}
